package com.qf.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lixu
 */
//@Component
public class CustomExceptionResolver implements HandlerExceptionResolver {

    /**
     * 处理异常  全局异常捕获   Dao Service Controller
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o ： 抛出异常的位置 public String com.qf.controller.ExceptionController.list() 抛出的异常
     * @param e ： 抛出的异常类型
     * @return
     */
    @Override
    public ModelAndView resolveException(
            HttpServletRequest httpServletRequest,
            HttpServletResponse httpServletResponse,
            Object o, Exception e) {
        System.out.println("o:" + o);
        System.out.println("e:" + e.getMessage());

        ModelAndView mav = new ModelAndView();
        if(e instanceof ArithmeticException){
            mav.addObject("error","离开了地球!不好意思");
            mav.setViewName("error");
        }else if(e instanceof RuntimeException){
            //
            mav.addObject("error","离开了地球!不好意思");
            mav.setViewName("error");
        }else{
            //
            mav.addObject("error","离开了地球!不好意思");
            mav.setViewName("error");
        }
        return mav;
    }
}
